package net.linestone.myapplication;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class ClientService {

	public static final String TAG_GET = "Get��ʽ";
	public static final String TAG_POST = "Post��ʽ";
	public static final String TAG_HTTPGET = "HttpGet��ʽ";
	public static final String TAG_HTTPPOST = "HttpPost��ʽ";
	public static final int HTTP_200 = 200;

	// Get��ʽ����
	public static void requestByGet() throws Exception {
		String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
		// �½�һ��URL����
		URL url = new URL(path);
		// ��һ��HttpURLConnection����
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		// �������ӳ�ʱʱ��
		urlConn.setConnectTimeout(5 * 1000);
		// ��ʼ����
		urlConn.connect();
		// �ж������Ƿ�ɹ�
		if (urlConn.getResponseCode() == HTTP_200) {
			// ��ȡ���ص����
			byte[] data = readStream(urlConn.getInputStream());
			Log.i(TAG_GET, "Get��ʽ����ɹ�������������£�");
			Log.i(TAG_GET, new String(data, "UTF-8"));
		} else {
			Log.i(TAG_GET, "Get��ʽ����ʧ��");
		}
		// �ر�����
		urlConn.disconnect();
	}

	// Post��ʽ����
	public static void requestByPost() throws Throwable {
		String path = "https://reg.163.com/logins.jsp";
		// ����Ĳ���ת��Ϊbyte����
		String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
				+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
		byte[] postData = params.getBytes();
		// �½�һ��URL����
		URL url = new URL(path);
		// ��һ��HttpURLConnection����
		HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
		// �������ӳ�ʱʱ��
		urlConn.setConnectTimeout(5 * 1000);
		// Post������������������
		urlConn.setDoOutput(true);
		// Post������ʹ�û���
		urlConn.setUseCaches(false);
		// ����ΪPost����
		urlConn.setRequestMethod("POST");
		urlConn.setInstanceFollowRedirects(true);
		// ��������Content-Type
		urlConn.setRequestProperty("Content-Type",
				"application/x-www-form-urlencode");
		// ��ʼ����
		urlConn.connect();
		// �����������
		DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
		dos.write(postData);
		dos.flush();
		dos.close();
		// �ж������Ƿ�ɹ�
		if (urlConn.getResponseCode() == HTTP_200) {
			// ��ȡ���ص����
			byte[] data = readStream(urlConn.getInputStream());
			Log.i(TAG_POST, "Post����ʽ�ɹ�������������£�");
			Log.i(TAG_POST, new String(data, "UTF-8"));
		} else {
			Log.i(TAG_POST, "Post��ʽ����ʧ��");
		}
	}

	// HttpGet��ʽ����
	public static void requestByHttpGet() throws Exception {
		String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
		// �½�HttpGet����
		HttpGet httpGet = new HttpGet(path);
		// ��ȡHttpClient����
		HttpClient httpClient = new DefaultHttpClient();
		// ��ȡHttpResponseʵ��
		HttpResponse httpResp = httpClient.execute(httpGet);
		// �ж��ǹ�����ɹ�
		if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
			// ��ȡ���ص����
			String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
			Log.i(TAG_HTTPGET, "HttpGet��ʽ����ɹ�������������£�");
			Log.i(TAG_HTTPGET, result);
		} else {
			Log.i(TAG_HTTPGET, "HttpGet��ʽ����ʧ��");
		}
	}

	// HttpPost��ʽ����
	public static void requestByHttpPost() throws Exception {
		String path = "https://reg.163.com/logins.jsp";
		// �½�HttpPost����
		HttpPost httpPost = new HttpPost(path);
		// Post����
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", "helloworld"));
		params.add(new BasicNameValuePair("pwd", "android"));
		// �����ַ�
		HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
		// ���ò���ʵ��
		httpPost.setEntity(entity);
		// ��ȡHttpClient����
		HttpClient httpClient = new DefaultHttpClient();
		// ��ȡHttpResponseʵ��
		HttpResponse httpResp = httpClient.execute(httpPost);
		// �ж��ǹ�����ɹ�
		if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
			// ��ȡ���ص����
			String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
			Log.i(TAG_HTTPGET, "HttpPost��ʽ����ɹ�������������£�");
			Log.i(TAG_HTTPGET, result);
		} else {
			Log.i(TAG_HTTPGET, "HttpPost��ʽ����ʧ��");
		}
	}

	// ��ȡ���ӷ��ص����
	private static byte[] readStream(InputStream inputStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		while ((len = inputStream.read(buffer)) != -1) {
			baos.write(buffer, 0, len);
		}
		byte[] data = baos.toByteArray();
		inputStream.close();
		baos.close();
		return data;
	}

}